<?php
	namespace Api\Controller;
	use Think\Controller;
	header("Content-Type:text/html;charset=utf-8");
	class ApplyController extends Controller
	{
		/* 
		 * 需求报名接口
		 *  
		 */
		public function applyPeople($uid=null, $dmid=null, $token=null)
		{
			if ($dmid==null || $uid==null || $token==null) 
			{
				$array1['result'] = 10401;
				$array1['message'] = "用户报名失败";
				echo json_encode($array1, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				if ($this->checkedToken($uid, $token))
				{
					//向需求表中报名字段添加用户
					$condition["dmid"] = $dmid;	
					$demandInfo = M("Demand") -> where($condition) -> find();
					if ($demandInfo["applypeople"] != null) 
					{
						$array = explode(",", $demandInfo["applypeople"]);
						if (in_array($uid, $array)) 
						{
							$array1['result'] = 10402;
							$array1['message'] = "用户已报名";
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
						else
						{
							array_push($array, $uid);
							$uidstring = implode(",", $array);
							//对数据库中的user表的fans
							$res = M("Demand") -> where($condition) ->setField("applypeople", $uidstring);
								
							
					     }
					}
					else
					{
						//向需求表中添加第一个报名的人
						$where["dmid"] = $dmid;
						/* $uids = $uids."-"; */
						$res = M("Demand") -> where($where) ->setField("applypeople", $uid);
							
					}
					//对是否报名成功进行判断
					if ($res > 0)
					{
						$condition1["uid"] = $uid;
						
						$dmAcceptInfo = M("User") -> where($condition1) -> getField("dmaccepted");
						if ($dmAcceptInfo == null) 
						{
							//向已接需求字段中添加第一条需求
							$result = M("User") -> where($condition1) -> setField("dmaccepted", $dmid);
						}
						else
						{
							//已接需求字段中已有需求
							$amArray = explode(",", $dmAcceptInfo);
							array_push($amArray, $dmid);
							$dmidstring = implode(",", $amArray);
							
							$result = M("User") -> where($condition1) -> setField("dmaccepted", $dmidstring);
						}
						//var_dump($dmAcceptInfo);
						if ($result > 0) 
						{
							$array1['result'] = 0;
							$array1['message'] = "用户报名成功";
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
						else
						{
							$array1['result'] = 10404;
							$array1['message'] = "用户报名失败";
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
						
					}
					else
					{
						$array1['result'] = 10403;
						$array1['message'] = "用户报名失败";
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
				}
				else
				{
					//var_dump($this->checkedToken($uid, $token));
					
					$array1['result'] = 10310;
					$array1['message'] = "用户报名失败";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
			}
		}
		
		/*
		 * 通告报名接口
		 *
		 */
		public function applyAnnunciate($uid=null, $id=null, $token=null)
		{
			if ($id==null || $uid==null || $token==null)
			{
// 				$acceptedData = array(
// 						"id"=>I("id"),
// 						"uid"=>I("uid"),
// 						"token"=>I("token")
// 				);
				$array1['result'] = 10401;
				$array1['message'] = "用户报名失败";
// 				$array1['acceptedData'] = $acceptedData;
				echo json_encode($array1, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				if ($this->checkedToken($uid, $token))
				{
					//向需求表中报名字段添加用户
					$condition["id"] = $id;
					$demandInfo = M("Annunciate") -> where($condition) -> find();
					if ($demandInfo["applypeople"] != null)
					{
						$array = explode(",", $demandInfo["applypeople"]);
						if (in_array($uid, $array))
						{
							$array1['result'] = 10402;
							$array1['message'] = "用户已报名";
							echo json_encode($array1, JSON_UNESCAPED_SLASHES);
							exit;
						}
						else
						{
							array_push($array, $uid);
							$uidstring = implode(",", $array);
							//对数据库中的user表的fans
							$res = M("Annunciate") -> where($condition) ->setField("applypeople", $uidstring);
		
							
						}
					}
					else
					{
						//向需求表中添加第一个报名的人
						$where["id"] = $id;
						/* $uids = $uids."-"; */
						$res = M("Annunciate") -> where($where) ->setField("applypeople", $uid);
							
					}
					
					if ($res > 0)
					{
						//首先查看该通告已报名人数
						$where1["id"] = $id;
						$applyUids = M("Annunciate") -> where($where1) -> getField("applypeople"); 
						if ($applyUids == null) 
						{
							$applyNumber = 0;
						}
						else
						{
							$UidsArray = explode(",", $applyUids);
							$applyNumber = count($UidsArray);
						}
						//查看登录用户的头像、uid、dtttype
						$condition1["uid"] = $uid;
						$userInfo = M("User") -> field("uid,type,rmtype,dtttype") -> where($condition1) -> find(); 
						if ($userInfo == null) 
						{
							//需要返回的数据
							$applyData = array(
									"uid"=>$uid,
									"headimg"=>"",
									"number"=>$applyNumber,
									"dtttype"=>0
							);
						}
						else
						{
							if ($userInfo["rmtype"] == 1) 
							{
								$userInfos = M("Userinfo") -> field("headimg,sex") ->where($condition1) -> find();
								if ($userInfos == null) 
								{
									$userInfo["headimg"] = "";
								}
								else
								{
									$userInfo["headimg"] = $userInfos["headimg"];
								}
							}
							else
							{
								$userInfo["headimg"] = "";
							}
							//var_dump($userInfos);
							//需要返回的数据
							$applyData = array(
									"uid"=>$userInfo["uid"],
									"headimg"=>$userInfo["headimg"],
									"number"=>$applyNumber,
									"dtttype"=>$userInfo["dtttype"]
							);
						}
						
						
						
						$array1['result'] = 0;
						$array1['message'] = "用户报名成功";
						$array1['userinfo'] = $applyData;
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
					else
					{
						$array1['result'] = 10403;
						$array1['message'] = "用户报名失败";
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
				}
				else
				{
					//var_dump($this->checkedToken($uid, $token));
						
					$array1['result'] = 10310;
					$array1['message'] = "用户报名失败";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
			}
		}
		
		/* 
		 *需求已报名接口 
		 *  
		 */
		public function applyedPeople($dmid=null)
		{
			if ($dmid == null) 
			{
				$array1['result'] = 10405;
				$array1['message'] = "查看已报名失败";
				echo json_encode($array1, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$where["dmid"] = $dmid;
				$peopleUids = M("Demand") -> where($where) -> getField("applypeople");
				
				if ($peopleUids == null) 
				{
					$array1['result'] = 10406;
					$array1['message'] = "暂无艺人报名";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
				else
				{
					$uidArray = explode(",", $peopleUids);
					//互换键值
					$turnArray = array_flip($uidArray);
					
					//查询结果
					$condition["uid"] = array("in", $uidArray);
					$peopleInfo = M("User") -> field("uid,nickname,type,rmtype,dtttype") -> where($condition) -> select();
					
					if ($peopleInfo > 0) 
					{
						foreach ($peopleInfo as $key=>$value)
						{
							//配合前面互换键值，使$result的键值和$uidArray一致
							$peopleInfos[$turnArray[$value['uid']]] = $value;
						}
						//按键排序
						ksort($peopleInfos);
					}
					else
					{
						$peopleInfos = array();
					}
					
					
					//var_dump($peopleInfos);
					foreach ($peopleInfos as $key=>$value)
					{
						if ($value["type"] == 0) 
						{
							//个人
							if ($value["rmtype"] == 1) 
							{
								//个人资料已完善
								unset($value["rmtype"]);
								$tagnameInfo = M("Userinfo") -> field("tagname,headimg") -> where("uid={$value['uid']}") -> find();
								if ($tagnameInfo["tagname"] == null) 
								{
									$tagname = array();
								}
								else
								{
									$tagname = explode("-", $tagnameInfo["tagname"]);
									
								}
								if ($tagnameInfo["headimg"] == null) 
								{
									$headimg = "";
								}
								else
								{
									$headimg = $tagnameInfo["headimg"];
								}
							}
							else
							{
								//个人资料未完善
								unset($value["rmtype"]);
								$tagname = array();
								$headimg = "";
							}
						}
						else
						{
							//企业
							if ($value["rmtype"] == 1)
							{
								//企业资料已完善
								unset($value["rmtype"]);
								$condition2["think_company.uid"] = $value["uid"];
								$tagnameInfo = M("Company") -> join("__USER__ on __COMPANY__.uid=__USER__.uid") -> field("tagname,cplogo") -> where($condition2) -> find();
								if ($tagnameInfo["tagname"] == null)
								{
									$tagname = array();
								}
								else
								{
									$tagname = explode("-", $tagnameInfo["tagname"]);
										
								}
								if ($tagnameInfo["cplogo"] == null)
								{
									$headimg = "";
								}
								else
								{
									$headimg = $tagnameInfo["cplogo"];
								}
							}
							else
							{
								//企业资料未完善
								unset($value["rmtype"]);
								$tagname = array();
								$headimg = "";
							}
							
						}
						$value["headimg"] = $headimg;
						$value["tagname"] = $tagname;
						$peopleInfos[$key] = $value;
					}
					
// 					var_dump($peopleInfo);
					if ($peopleInfos == null) 
					{
						$array1['result'] = 10407;
						$array1['message'] = "查看已报名失败";
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
					else
					{
						$array1['result'] = 0;
						$array1['message'] = "查看已报名成功";
						$array1["peoplelist"] = $peopleInfos;
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
				}
			}
		}
		
		/*
		 *通告已报名接口
		 *
		 */
		public function annuciatePeople($id=null)
		{
			if ($id == null)
			{
				$array1['result'] = 10405;
				$array1['message'] = "查看已报名失败";
				echo json_encode($array1, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$where["id"] = $id;
				$peopleUids = M("Annunciate") -> where($where) -> getField("applypeople");
		
				if ($peopleUids == null)
				{
					$array1['result'] = 10406;
					$array1['message'] = "暂无艺人报名";
					echo json_encode($array1, JSON_UNESCAPED_SLASHES);
					exit;
				}
				else
				{
					$uidArray = explode(",", $peopleUids);
					//互换键值
					$turnArray = array_flip($uidArray);
					
					$condition["uid"] = array("in", $uidArray);
					$peopleInfo = M("User") -> field("uid,nickname,type,rmtype,dtttype") -> where($condition) -> select();
					//var_dump($peopleInfo);
					
					if ($peopleInfo > 0)
					{
						foreach ($peopleInfo as $key=>$value)
						{
							//配合前面互换键值，使$result的键值和$uidArray一致
							$peopleInfos[$turnArray[$value['uid']]] = $value;
						}
						//按键排序
						ksort($peopleInfos);
					}
					else
					{
						$peopleInfos = array();
					}
					
					foreach ($peopleInfos as $key=>$value)
					{
						if ($value["type"] == 0)
						{
							//个人
							if ($value["rmtype"] == 1)
							{
								//个人资料已完善
								$tagnameInfo = M("Userinfo") -> field("tagname,headimg") -> where("uid={$value['uid']}") -> find();
								if ($tagnameInfo["tagname"] == null)
								{
									$tagname = array();
								}
								else
								{
									$tagname = explode("-", $tagnameInfo["tagname"]);
										
								}
								if ($tagnameInfo["headimg"] == null)
								{
									$headimg = "";
								}
								else
								{
									$headimg = $tagnameInfo["headimg"];
								}
							}
							else
							{
								//个人资料未完善
								$tagname = array();
								$headimg = "";
							}
						}
						else
						{
							//企业
							if ($value["rmtype"] == 1)
							{
								//企业资料已完善
								$tagnameInfo = M("Company") -> field("tagname,cplogo") -> where("uid={$value['uid']}") -> find();
								if ($tagnameInfo["tagname"] == null)
								{
									$tagname = array();
								}
								else
								{
									$tagname = explode("-", $tagnameInfo["tagname"]);
		
								}
								if ($tagnameInfo["cplogo"] == null)
								{
									$headimg = "";
								}
								else
								{
									$headimg = $tagnameInfo["cplogo"];
								}
							}
							else
							{
								//企业资料未完善
								$tagname = array();
								$headimg = "";
							}
								
						}
						$value["headimg"] = $headimg;
						$value["tagname"] = $tagname;
						
						$peopleInfos[$key] = $value;
					}
						
// 					var_dump($peopleInfos);
					if ($peopleInfos == null)
					{
						$array1['result'] = 10407;
						$array1['message'] = "查看已报名失败";
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
					else
					{
						$array1['result'] = 0;
						$array1['message'] = "查看已报名成功";
						$array1["peoplelist"] = $peopleInfos;
						echo json_encode($array1, JSON_UNESCAPED_SLASHES);
						exit;
					}
				}
			}
		}
		
		/*
		 *验证token的方法
		 *
		 */
		protected function checkedToken($uid=null, $token=null)
		{
			if ($uid==null || $token==null)
			{
				return false;
			}
			else
			{
				$where["uid"] = $uid;
				$tokeninfo = M("User") -> where($where) -> getField("token");
				
				if ($token == $tokeninfo)
				{
					return true;
				}
				else
				{
					return false;
				}
			}
		}
	}